package com.dsrk.sysusage;

import java.sql.*;
import javax.naming.*;
import javax.sql.*;

public class Gather {
	private DataSource ds = null;

	private java.sql.Connection conn = null;

	private Statement st = null;

	public Gather() {
		try {
			InitialContext ctx = new InitialContext();
			if (ds == null)
				ds = (DataSource) ctx.lookup("java:comp/env/jdbc/MyDataSource");
			if (conn == null)
				conn = ds.getConnection();
			if (st == null)
				st = conn.createStatement();
		} catch (NamingException e) {
			System.err.println(e);
			e.printStackTrace();
		} catch (SQLException e) {
			System.err.println(e);
			e.printStackTrace();
		}

	}

	public int echo(int i) {
		return i;
	}

	public int add(int i1, int i2) {
		return i1 + i2;
	}

	public int insertEMP(int i1) {
		int result = -999;

		try {
			st.executeUpdate("INSERT INTO EMP(NUM) VALUES (" + i1 + ")");
			ResultSet rs = st.executeQuery("SELECT COUNT(*) FROM EMP");
			while (rs.next()) {
				result = rs.getInt(1);
				System.out.println("EMP = " + result);
			}
		} catch (SQLException e) {
			System.err.println(e);
			e.printStackTrace();
		}

		return result;
	}

	public int insert_group(String groupname, int level) {
		try {
			InitialContext ctx = new InitialContext();
			DataSource ds = (DataSource) ctx
					.lookup("java:comp/env/jdbc/MyDataSource");
			java.sql.Connection conn = ds.getConnection();

			Statement st = conn.createStatement();
			st.executeUpdate("INSERT INTO sysusage_group VALUES ('" + groupname
					+ "'," + level + ")");
		} catch (NamingException e) {
			System.err.println(e);
			e.printStackTrace();
		} catch (SQLException e) {
			System.err.println(e);
			e.printStackTrace();
		}
		return 999;
	}

	public int insert_server(String hostname, String groupname,
			String servertype) {
		try {
			InitialContext ctx = new InitialContext();
			DataSource ds = (DataSource) ctx
					.lookup("java:comp/env/jdbc/MyDataSource");
			java.sql.Connection conn = ds.getConnection();

			Statement st = conn.createStatement();
			st.executeUpdate("INSERT INTO sysusage_server VALUES ('" + hostname
					+ "','" + groupname + "','" + servertype + "')");
		} catch (NamingException e) {
			System.err.println(e);
			e.printStackTrace();
		} catch (SQLException e) {
			System.err.println(e);
			e.printStackTrace();
		}
		return 999;
	}

	public int insert_rawdata(String hostname, String property, int value,
			String timestamp) {
		if (!timestamp.equals("CURRENT_TIMESTAMP")) {
			timestamp = "'" + timestamp + "'";
		}

		try {
			String query = "INSERT INTO sysusage_rawdata " + " VALUES ("
			+ timestamp + ", '" + hostname + "','" + property + "',"
			+ value + ")";
			
			System.out.println("=== insert_rawdata's Query : " + query);
			st.executeUpdate(query);
		} catch (SQLException e) {
			System.err.println(e);
			e.printStackTrace();
		}

		return 0;
	}

	public int insert_rawdata(String hostname, String property, int value) {
		insert_rawdata(hostname, property, value, "CURRENT_TIMESTAMP");

		return 0;
	}

	public int executeQuery(String sql) {
		int result = -1;

		try {
			ResultSet rs = st.executeQuery(sql);

			while (rs.next()) {
				result = rs.getInt(1);
				System.out.println("Result = " + result);
			}
		} catch (SQLException e) {
			System.err.println(e);
			e.printStackTrace();
		}

		return result;
	}
}
